#!/bin/bash

# Mini-gRPC 项目构建脚本
# 用于编译和构建整个项目

set -e  # 遇到错误时退出

echo "=========================================="
echo "Mini-gRPC 项目构建脚本"
echo "=========================================="

# 检查Java版本
echo "检查Java版本..."
java -version

# 检查Maven版本
echo "检查Maven版本..."
mvn --version

echo ""
echo "开始构建项目..."

# 清理项目
echo "1. 清理项目..."
mvn clean

# 编译core模块
echo "2. 编译mini-grpc-core模块..."
mvn compile -pl mini-grpc-core

# 安装core模块到本地仓库
echo "3. 安装mini-grpc-core到本地仓库..."
mvn install -pl mini-grpc-core -DskipTests

# 编译protoc模块
echo "4. 编译mini-grpc-protoc模块..."
mvn compile -pl mini-grpc-protoc

# 安装protoc模块到本地仓库
echo "5. 安装mini-grpc-protoc到本地仓库..."
mvn install -pl mini-grpc-protoc -DskipTests

# 编译spring模块
echo "6. 编译mini-grpc-spring模块..."
mvn compile -pl mini-grpc-spring

# 安装spring模块到本地仓库
echo "7. 安装mini-grpc-spring到本地仓库..."
mvn install -pl mini-grpc-spring -DskipTests

# 编译examples模块
echo "8. 编译mini-grpc-examples模块..."
mvn compile -pl mini-grpc-examples

# 运行测试（可选）
if [ "$1" = "--with-tests" ]; then
    echo "9. 运行测试..."
    mvn test
else
    echo "9. 跳过测试（使用 --with-tests 参数来运行测试）"
fi

# 打包整个项目
echo "10. 打包整个项目..."
mvn package -DskipTests

echo ""
echo "=========================================="
echo "构建完成！"
echo "=========================================="
echo ""
echo "生成的文件："
echo "- mini-grpc-core/target/mini-grpc-core-1.0.0-SNAPSHOT.jar"
echo "- mini-grpc-protoc/target/mini-grpc-protoc-1.0.0-SNAPSHOT.jar"
echo "- mini-grpc-spring/target/mini-grpc-spring-1.0.0-SNAPSHOT.jar"
echo "- mini-grpc-examples/target/mini-grpc-examples-1.0.0-SNAPSHOT.jar"
echo ""
echo "使用方法："
echo "  ./build.sh              # 编译项目（跳过测试）"
echo "  ./build.sh --with-tests # 编译项目并运行测试"
echo "" 